
package com.zsrt.jmj.teacher.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zsrt.jmj.student.dao.StudentDao;
import com.zsrt.jmj.student.dao.impl.StudentDaoImpl;
import com.zsrt.jmj.teacher.dao.TeacherDao;
import com.zsrt.jmj.teacher.dao.impl.TeacherDaoImpl;
import com.zsrt.jmj.teacher.entity.TeacherEntity;
import com.zsrt.jmj.teacher.service.TeacherService;

public class TeacherServiceImpl implements TeacherService {

	private TeacherDao dao= new TeacherDaoImpl();
	private StudentDao sDao= new StudentDaoImpl();
	@Override
	public void add(TeacherEntity entity, Map<String, Object> map) {
		//先进行入参验证
		//验证工资
		double d = entity.getSalary();
		String sa = String.valueOf(d);
		String regEx = "(^\\d{1,8}(?:\\.\\d{0,2})?$)";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(sa);
		boolean rs = matcher.find();
		
		if(rs){
			//验证名称是否为空
			if(entity.getTeacherName() != null){
				try {
					dao.add(entity);
					map.put("result", "success");
					map.put("jieguo", "添加成功...");
					return;
				} catch (Exception e) {
					map.put("result", "failure");
					map.put("jieguo", "添加失败...");
					e.printStackTrace();
					return;
				}
			}else{
				map.put("result", "failure");
				map.put("jieguo", "添加失败,教师姓名不能为空...");
				return;
			}
		}else{
			map.put("result", "failure");
			map.put("jieguo", "添加失败，工资最多为两位小数...");
			return;
		}
		
	}
	@Override
	public void findAllTeachers(Map<String, Object> map) {
		
		try {
			List<TeacherEntity> list = dao.findAllTeachers();
			map.put("result", "success");
			map.put("jieguo", "查询成功...");
			map.put("data", list);
		} catch (Exception e) {
			map.put("result", "failure");
			map.put("jieguo", "查询失败...");
			e.printStackTrace();
		}
		
	}
	@Override
	public void updateById(TeacherEntity entity, Map<String, Object> map) {
		
		//验证工资
		double d = entity.getSalary();
		String sa = String.valueOf(d);
		String regEx = "(^\\d{1,8}(?:\\.\\d{0,2})?$)";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(sa);
		boolean rs = matcher.find();
		
		if(rs){
			//验证名称是否为空
			if(entity.getTeacherName() != null){
				try {
					dao.updateById(entity);
					map.put("result", "success");
					map.put("jieguo", "修改成功...");
					return;
				} catch (SQLException e) {
					map.put("result", "failure");
					map.put("jieguo", "修改失败...");
					e.printStackTrace();
					return;
				}
			}else {
				map.put("result", "failure");
				map.put("jieguo", "修改失败...");
				return;
			}
		}else {
			map.put("result", "failure");
			map.put("jieguo", "修改失败...");
			return;
		}
			
			
		
		
	}
	@Override
	public void findTeacherById(String id, Map<String, Object> map) {
		int tId = Integer.parseInt(id);
		try {
			TeacherEntity entity = dao.findTeacherById(tId);
			map.put("result", "success");
			map.put("jieguo", "查询成功...");
			map.put("data", entity);
		} catch (SQLException e) {
			map.put("result", "failure");
			map.put("jieguo", "查询失败...");
			e.printStackTrace();
		}
		
	}
	@Override
	public void deleteByIds(String ids, Map<String, Object> map) {
		String[] split = ids.split(",");
		for(int i=0;i<split.length;i++){
			String id = split[i];
			int cId = Integer.parseInt(id);
			try {
				
				//在删除老师之前必须先删除该老师下面的学生
				sDao.deleteByTeacherId(cId);
				dao.deleteById(cId);
				map.put("result", "success");
				map.put("jieguo", "删除成功...");
			} catch (SQLException e) {

				map.put("result", "failure");
				map.put("jieguo", "删除失败...");
				e.printStackTrace();
				return;
			}
		}
		
	}

}
